package com.taotao.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.JsonUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.pojo.TbItemParamItemExample.Criteria;
import com.taotao.rest.redis.JedisClient;
import com.taotao.rest.service.ItemService;
@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private TbItemMapper itemMapper;
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;
	
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper paramItemMapper;
	//商品基本信息
	@Override
	public TaotaoResult getItemBaseInfo(long itemId) {
		//从redis缓存中去数据
		try {
			String json=jedisClient.get(REDIS_ITEM_KEY+":"+itemId+":base");
			if(!StringUtils.isBlank(json)){
				TbItem item=JsonUtils.jsonToPojo(json,TbItem.class);
				return TaotaoResult.ok(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		TbItem item=itemMapper.selectByPrimaryKey(itemId);
		
		//存入redis缓存中
		try {
			jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":base", JsonUtils.objectToJson(item));
			jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":base", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok(item);
	}
	
	//商品描述信息
	@Override
	public TaotaoResult getItemDesc(long itemId) {
		//取redis缓存
		try {
			String json=jedisClient.get(REDIS_ITEM_KEY+":"+itemId+":desc");
			if(!StringUtils.isBlank(json)){
				TbItemDesc itemDesc=JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return TaotaoResult.ok(itemDesc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		TbItemDesc itemDesc=itemDescMapper.selectByPrimaryKey(itemId);
		//添加redis缓存
		try {
			jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":desc", JsonUtils.objectToJson(itemDesc));
			jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":desc", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return TaotaoResult.ok(itemDesc);
	}
	
	//商品规格参数
	@Override
	public TaotaoResult getItemParam(long itemId) {
		//取redis缓存
		try {
			String json=jedisClient.get(REDIS_ITEM_KEY+":"+itemId+":param");
			if(!StringUtils.isBlank(json)){
				TbItemParamItem paramItem=JsonUtils.jsonToPojo(json, TbItemParamItem.class);
				return TaotaoResult.ok(paramItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		TbItemParamItemExample paramItem=new TbItemParamItemExample();
		Criteria criteria = paramItem.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> paramItems = paramItemMapper.selectByExampleWithBLOBs(paramItem);
		if(paramItems!=null&&paramItems.size()>0){
			TbItemParamItem ItemparamItem=paramItems.get(0);
			//添加redis缓存
			try {
				jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":param",JsonUtils.objectToJson(ItemparamItem));
				jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":param", REDIS_ITEM_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return TaotaoResult.ok(ItemparamItem);
		}
		return TaotaoResult.build(400, "无此商品规格参数");
	}

}
